<template>
  <div id="app" v-cloak>
		<transition name="fade"
		            mode="out-in">
			<router-view></router-view>
		</transition>
  </div>
</template>

<script>
//let Base64 = require('js-base64').Base64;
import Svc from './service/index.js'
export default {
  name: 'app',
	methods: {
			fnGetParam(list) {
				var gw = ''
				for(var i = 0; i < list.length; i++) {
					var item = list[i].split("=")
					if(item[0] == 'gw') {
//						gw = Base64.decode(item[1])
						gw = item[1]
						break
					}
				}
				return gw
			},
    	//显示message提示框
	    openMessage : function(msg,type){
	        this.$message({
	          	showClose: true,
	          	message: msg,
	          	type: type
	        });
	    },
        //判断菜单是否展示
        fnShowMenu(data){
            var routes = this.$router.options.routes
            //当前用户为审核人
            if(data.IsApprover) {
                for(var i = 0; i < routes.length; i++) {
                    var item = routes[i]
                    if(item.iconCls ==="el-icon-custom-managerment"
                        || item.iconCls ==="el-icon-custom-apply"
                        || item.iconCls ==="el-icon-custom-my-managerment"
                        || item.iconCls ==="el-icon-custom-my-apply") {
                        item.hidden = false
                        var child = item.children || []
                        for(var j = 0; j < child.length; j++) {
                            child[j].hidden = false
                        }
                    }
                }
            }

            //当前用户为管理员
            if(data.IsAdmin) {
                for(var i = 0; i < routes.length; i++) {
                    var item = routes[i]
                    if(item.path === "/home" && item.iconCls !== "el-icon-custom-managerment2") {
                        item.hidden = false
                        var child = item.children || []
                        for(var j = 0; j < child.length; j++) {
                            child[j].hidden = false
                        }
                    }
                }
            }

            //当前用户为后勤人员
            if(data.IsLogistics) {
                for(var i = 0; i < routes.length; i++) {
                    var item = routes[i]
                    if(item.iconCls ==="el-icon-custom-managerment2"
                        || item.iconCls ==="el-icon-custom-apply"
                        || item.iconCls ==="el-icon-custom-my-apply"
                        || item.iconCls ==="el-icon-custom-search"
                        || item.iconCls === "el-icon-custom-report") {
                        item.hidden = false
                        var child = item.children || []
                        for(var j = 0; j < child.length; j++) {
                            child[j].hidden = false
                        }
                    }
                }
            }

            //当前用户非管理员、非审核人
            if (!data.IsAdmin && !data.IsApprover && !data.IsLogistics) {
                for(var i = 0; i < routes.length; i++) {
                    var item = routes[i]
                    if(item.iconCls ==="el-icon-custom-apply" || item.iconCls ==="el-icon-custom-my-apply") {
                        item.hidden = false
                        var child = item.children || []
                        for(var j = 0; j < child.length; j++) {
                            child[j].hidden = false
                        }
                    }
                }
            }
        }
		},
	mounted() {
		var that = this
		var param = window.location.search.substring(1)
		if(param) {//从其他网址跳转过来->已登录->跳转到Home页
			if (!sessionStorage.getItem('user')) {
					var gw = that.fnGetParam(param.split('&'))
					var empid = gw.split('&')[0]
			    Svc.fnGetData('/api/account/user/'+empid,{},function(res){
			    	if (res.body.ErrCode == 0){
		    			//保存用户信息
		    			sessionStorage.setItem('user', JSON.stringify(res.body.Data));
                        //菜单展示
                        that.fnShowMenu(res.body.Data)
                        // 判断是否为后勤登录，跳转到不同的待办事项页面
                        if (res.body.Data.IsLogistics) {
                            that.$router.push({ path: '/sign' });
                        } else {
                            that.$router.push({path: '/mealmanagement'});
                        }
			    	} else {
			    		that.openMessage(res.body.ErrMsg,'error')
			    	}
			    },function(res){
			    	that.openMessage("网络错误，请重新！",'error')
			    })
			} else {
			    var userData = JSON.parse(sessionStorage.getItem('user'))
                //菜单展示
                that.fnShowMenu(userData)
				// 判断是否为后勤登录，跳转到不同的待办事项页面
				if (userData.IsLogistics) {
					that.$router.push({ path: '/sign' });
				} else {
					that.$router.push({path: '/mealmanagement'});
				}
			}
		} else {
			if (!sessionStorage.getItem('user')) {
					//未登录->跳转到登录页
					that.$router.push({ path: '/login' });
			}
		}
	}
}
</script>

<style scoped>
	[v-cloak] {
		display: none;
	}

	body {
		margin: 0px;
		padding: 0px;
		font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, SimSun, sans-serif;
		font-size: 14px;
		-webkit-font-smoothing: antialiased;
	}
	
	#app {
		position: absolute;
		top: 0px;
		bottom: 0px;
		left: 0;
		right: 0;
		width: 100%;
		height: 100%;
	}
</style>
